package com.link2room.rodimus.facade.impl;

import com.link2room.common.exception.SysException;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.L2RAssert;
import com.link2room.common.util.StringUtil;
import com.link2room.rodimus.dto.param.inside.FindSalesmanBySalesmanNoPararmDto;
import com.link2room.rodimus.dto.result.base.SalesmanMappingDto;
import com.link2room.rodimus.entity.SalesmanMappingEntity;
import com.link2room.rodimus.entity.SalesmanSourceDataEntity;
import com.link2room.rodimus.facade.ISalesmanInsideFacadeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.link2room.enums.exception.RodimusSysExceptionEnum.FIND_SALESMAN_BY_SALESMAN_NO_ERROR;

/**
 * Created by LN on 2017/12/26
 */
@Component("salesmanInsideFacadeService")
public class SalesmanInsideFacadeServiceImpl implements ISalesmanInsideFacadeService {

    @Resource
    private ICommonService commonService;

    @Override
    public SalesmanMappingDto findSalesmanBySalesmanNo(FindSalesmanBySalesmanNoPararmDto pararmDto) throws Exception {
        String salesmanNo = pararmDto.getSalesmanNo();
        L2RAssert.isTrue(StringUtil.isBlank(salesmanNo), new SysException(FIND_SALESMAN_BY_SALESMAN_NO_ERROR, "[salesmanNo] is required."));
        SalesmanMappingEntity find = new SalesmanMappingEntity();
        find.setSalesmanNo(salesmanNo);
        SalesmanMappingEntity qers = commonService.findOneEQ(SalesmanMappingEntity.class, find);
        if (null == qers) {
            return null;
        } else {
            SalesmanSourceDataEntity querySSDE=new SalesmanSourceDataEntity();
            querySSDE.setEmployeeNo(qers.getEmployeeNo());
            SalesmanSourceDataEntity salesmanSourceDataEntity = commonService.findOneEQ(SalesmanSourceDataEntity.class, querySSDE);
            if(salesmanSourceDataEntity!=null){
                SalesmanMappingDto salesmanMappingDto=ClassConverUtil.copyProperties(qers,SalesmanMappingDto.class);
                salesmanMappingDto.setEmployeeName(salesmanSourceDataEntity.getName());
                return salesmanMappingDto;
            }else {
                return null;
            }
        }
    }
}
